Check for TermInfo availability to see if color output is possible
authorMike Boutin <mike.boutin@gmail.com>
Sun, 2 Aug 2015 01:09:52 +0000 (21:09 -0400)
committerMike Boutin <mike.boutin@gmail.com>
Tue, 4 Aug 2015 23:03:00 +0000 (19:03 -0400)
before attempting to create a colored terminal.

src/cargo/core/shell.rs

index 227261c8c30e92e70a99a6f13568600c68af5f05..413c9aa08bad738fbd7d6c6b3ac3c51284eb7375 100644 (file)
@@ -144,18 +144,22 @@ impl MultiShell {
 
 impl Shell {
     pub fn create(out: Box<Write + Send>, config: ShellConfig) -> Shell {
-        // Check for cfg!(windows) as colored output on Windows can only be supported when a tty is
-        // present.
-        if !cfg!(windows) || config.tty {
-            let term = TerminfoTerminal::new(out);
-            term.map(|t| Shell {
-                terminal: Colored(Box::new(t)),
-                config: config
-            }).unwrap_or_else(|| {
+        match ::term::terminfo::TermInfo::from_env() {
+            Ok(ti) => {
+                // Color output is possible.
+                Shell {
+                    terminal: Colored(Box::new(TerminfoTerminal::new_with_terminfo(out, ti))),
+                    config: config
+                }
+            }
+            _ if config.tty => {
+                // Color output is expected but not available, fall back to stderr.
                 Shell { terminal: NoColor(Box::new(io::stderr())), config: config }
-            })
-        } else {
-            Shell { terminal: NoColor(out), config: config }
+            }
+            _ => {
+                // No color output.
+                Shell { terminal: NoColor(out), config: config }
+            }
         }
     }